const path = require("path");
const persistentDBPath = path.join(__dirname, "./users.bfd");
const Datastore = require("nedb-promises");
const db = Datastore.create(persistentDBPath);
db.persistence.setAutocompactionInterval(1000 * 60 * 30);



async function addUser(user) {
    let doc = await db.findOne({ 'secUid': user.secUid });
    if (doc) {
        await db.remove({ 'secUid': user.secUid },  { multi: true }, function (err, numRemoved) {
        });
    }

    await db.insert(user);
    doc = await db.findOne({ 'secUid': user.secUid })
    return doc;
}

async function findAll() {
    let doc = await db.find({});
    // console.log(doc);
    return doc;
}

module.exports = {
    addUser,findAll
}